package com.zige.zige.dialog;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.umeng.socialize.UMShareAPI;
import com.zige.zige.R;


import java.util.ArrayList;

/**
 * Created by Fei on 2015/11/5.
 * 分享弹出框
 */
public class ZiGeShareDialog extends DialogFragment {

    private Activity activity;
    private View view;
    private GridView shareGridview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = getActivity();
        images.add(R.drawable.share_dialog_img_sinaweibo);
        images.add(R.drawable.share_dialog_img_weixin);
        images.add(R.drawable.share_dialog_img_pengyouquan);
        images.add(R.drawable.share_dialog_img_xiaobanma);

        names.add("新浪微博");
        names.add("微信好友");
        names.add("朋友圈");
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        view = View.inflate(activity, R.layout.dialog_zige_share, null);
        GridView gv = (GridView) view.findViewById(R.id.gv);
        gv.setAdapter(new Adapter());
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        zigeshareLister.shareSina();
                        dismiss();
                        break;
                    case 1:
                        zigeshareLister.shareWeiXin();
                        dismiss();
                        break;
                    case 2:
                        zigeshareLister.shareWeiXinCircle();
                        dismiss();
                        break;
                }
            }
        });
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        window.setGravity(Gravity.CENTER);
        window.setAttributes(lp);
        getDialog().show();

        return view;


    }

    ArrayList<Integer> images = new ArrayList<>();
    ArrayList<String> names = new ArrayList<>();

    private class Adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return names.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(getActivity(), R.layout.share_item, null);
            ((ImageView) view.findViewById(R.id.img)).setImageResource(images.get(position));
            ((TextView) view.findViewById(R.id.name)).setText(names.get(position));
            return view;
        }
    }

    public interface ZigeshareLister {
        void shareSina();
        void shareWeiXin();
        void shareWeiXinCircle();
    }

    private  ZigeshareLister zigeshareLister;
    public void setSharelister(ZigeshareLister lister) {
        this.zigeshareLister = lister;
    }
}

